package com.qys.livesMall.app.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.sys.LimitUrlSaveDTO;
import com.qys.livesMall.app.user.model.dto.sys.SysLimitUrlPageDTO;
import com.qys.livesMall.app.user.model.entity.LimitUrl;
import com.qys.livesMall.app.user.mapper.LimitUrlMapper;
import com.qys.livesMall.app.user.model.vo.sys.LimitUrlVO;
import com.qys.livesMall.app.user.service.LimitUrlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.cache.RedisCache;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.framework.utils.SecurityUtils;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;

import static com.qys.livesMall.app.user.constants.RedisConstant.LIMIT_URLS;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2023-07-31
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LimitUrlServiceImpl extends ServiceImpl<LimitUrlMapper, LimitUrl> implements LimitUrlService {

    private final RedisCache redisCache;

    @Override
    public Page<LimitUrlVO> pageBySys(SysLimitUrlPageDTO queryDTO) {
        Page<LimitUrlVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return baseMapper.pageBySys(page, queryDTO);
    }

    @Override
    public void saveOrUpdate(LimitUrlSaveDTO saveDTO) {
        log.info("管理员增接口权限验证,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), saveDTO);
        LimitUrl limitUrl = new LimitUrl();
        BeanUtils.copyProperties(saveDTO, limitUrl);
        if (null == saveDTO.getId()) {
            //save
            limitUrl.setDeleted((byte) 0);
            limitUrl.setStatus((byte) 0);
            limitUrl.setCreateTime(LocalDateTime.now());
        } else {
            //update
            limitUrl.setUpdateTime(LocalDateTime.now());
        }
        saveOrUpdate(limitUrl);
        init();
    }

    @Override
    public void deleteById(Long id) {
        log.info("管理员删除接口权限验证,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), id);
        baseMapper.deleteById(id);
        //初始化更新
        init();
    }

    @Override
    public void shelves(Long id) {
        log.info("管理员执行接口权限验证上下架操作,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), id);
        LimitUrl limitUrl = baseMapper.selectById(id);
        if (null == limitUrl) {
            throw new QysException("请检查ID");
        }
        limitUrl.setStatus((byte) (limitUrl.getStatus() == 0 ? 1 : 0));
        baseMapper.updateById(limitUrl);
        //初始化更新
        init();
    }

    @Override
    public LimitUrlVO queryById(Long id) {
        return BeanUtil.copyProperties(getById(id), LimitUrlVO.class);
    }


    @Override
    @PostConstruct
    public void init() {
        List<LimitUrl> list = list(Wrappers.<LimitUrl>lambdaQuery().eq(LimitUrl::getStatus, 0));
        if (list == null || list.size() == 0) {
            return;
        }
        redisCache.setCacheObject(LIMIT_URLS,list);
    }

}
